package vapi_v1

import (
	"context"
	"errors"
	"gf-xb-api/internal/dao"
	"gf-xb-api/internal/model/do"
	"gf-xb-api/internal/model/dto"
	"gf-xb-api/internal/model/entity"

	"github.com/gogf/gf/v2/util/gconv"
)

func (s *sVApi) Create(ctx context.Context, in dto.CreateApiInput) (output bool, err error) {

	count, err := dao.SysApi.Ctx(ctx).Count(do.SysApi{
		Route:  in.Route,
		Method: in.Method,
	})

	if err != nil {
		return false, err
	}

	if count > 0 {
		return false, errors.New("Route和Method已被使用")
	}

	var data *entity.SysApi
	if err = gconv.Struct(in, &data); err != nil {
		return false, err
	}

	_, err = dao.SysApi.Ctx(ctx).Insert(data)
	if err != nil {
		return false, err
	}

	return true, nil
}
